clear all
set more off
set mem 100m
tempfile temp

/* 

This dofile categorizes trust variables using raw data from ENAHO's module on democracy  (gobernabilidad democracia y transparencia, modulo 85)


*/


local enaho ../data/enaho
local working ../working


** 2004-2006
forvalues x = 2004/2006 {
	use `enaho'/`x'/enaho01b-`x'-1, clear
	gen year = `x'
	
	qui recode p2_* (1 2 = 0) (3 4 = 1) (else = .)
	rename p2_1 trust_prov
	rename p2_2 trust_dist
	rename p2_3 trust_police
	rename p2_4 trust_army
	rename p2_5 trust_newsp
	rename p2_6 trust_rtv
	rename p2_7 trust_parties
	rename p2_10 trust_regi
	rename p2_12 trust_jury
	rename p2_18 trust_parliament

	gen weightg = facgob
	
	destring conglome vivienda hogar codperso, replace
	keep conglome vivienda hogar codperso year trust_* weightg
	save trust_`x', replace

}	

** 2007-2016
forvalues x = 2007/2016 {
	use `enaho'/`x'/enaho01b-`x'-1, clear
	gen year = `x'
	
	qui recode p1_* (1 2 = 0) (3 4 = 1) (5 = .) (else = .)
	rename p1_01 trust_jne
	rename p1_02 trust_onpe
	rename p1_06 trust_police
	rename p1_07 trust_army
	rename p1_08 trust_regi
	rename p1_04 trust_prov
	rename p1_05 trust_dist	
	rename p1_09 trust_jury	
	rename p1_12 trust_parliament	
	rename p1_13 trust_parties	
	rename p1_14 trust_newsp	
	rename p1_15 trust_rtv		
	rename p1_16 trust_church	
	
	cap gen weightg = factor07
	cap gen weightg = facgob07
	cap gen weightg = famiegob07
	
	destring conglome vivienda hogar codperso, replace force
	keep conglome vivienda hogar codperso year trust_* weightg
	save trust_`x', replace
}

use trust_2004, clear
forvalues x = 2005/2016 {
	append using trust_`x'
}	

forvalues x = 2004/2016 {
	erase trust_`x'.dta
}	

*egen miss = rowtotal(trust_*), missing
*drop if miss == .
*drop miss

** SAVE
compress
save `working'\trust, replace
